package com.nanhai.buddhist.college.module.collegebase.service.accommodationbed;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nanhai.buddhist.college.framework.common.util.collection.CollectionUtils;
import com.nanhai.buddhist.college.framework.mybatis.core.dataobject.BaseDO;
import com.nanhai.buddhist.college.framework.mybatis.core.query.LambdaQueryWrapperX;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;

import java.util.*;

import com.nanhai.buddhist.college.module.collegebase.controller.admin.accommodationbed.vo.*;
import com.nanhai.buddhist.college.module.collegebase.dal.dataobject.accommodationbed.AccommodationBedDO;
import com.nanhai.buddhist.college.framework.common.pojo.PageResult;

import com.nanhai.buddhist.college.module.collegebase.convert.accommodationbed.AccommodationBedConvert;
import com.nanhai.buddhist.college.module.collegebase.dal.mysql.accommodationbed.AccommodationBedMapper;

import static com.nanhai.buddhist.college.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.nanhai.buddhist.college.module.collegebase.enums.ErrorCodeConstants.*;

/**
 * 床铺 Service 实现类
 *
 * @author AKai
 */
@Service
@Validated
public class AccommodationBedServiceImpl implements AccommodationBedService {

    @Resource
    private AccommodationBedMapper accommodationBedMapper;

    @Override
    public String createAccommodationBed(AccommodationBedCreateReqVO createReqVO) {
        // 插入
        AccommodationBedDO accommodationBed = AccommodationBedConvert.INSTANCE.convert(createReqVO);
        accommodationBedMapper.insert(accommodationBed);
        // 返回
        return accommodationBed.getId();
    }

    @Override
    public void updateAccommodationBed(AccommodationBedUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateAccommodationBedExists(updateReqVO.getId());
        // 更新
        AccommodationBedDO updateObj = AccommodationBedConvert.INSTANCE.convert(updateReqVO);
        accommodationBedMapper.updateById(updateObj);
    }

    @Override
    public void deleteAccommodationBed(String id) {
        // 校验存在
        this.validateAccommodationBedExists(id);
        // 删除
        accommodationBedMapper.deleteById(id);
    }

    private void validateAccommodationBedExists(String id) {
        if (accommodationBedMapper.selectById(id) == null) {
            throw exception(ACCOMMODATION_BED_NOT_EXISTS);
        }
    }

    @Override
    public AccommodationBedDO getAccommodationBed(String id) {
        return accommodationBedMapper.selectById(id);
    }

    @Override
    public List<AccommodationBedDO> getAccommodationBedList(Collection<String> ids) {
        return CollectionUtils.isEmpty(ids) ? Collections.emptyList() : accommodationBedMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<AccommodationBedDO> getAccommodationBedPage(AccommodationBedPageReqVO pageReqVO) {
        return accommodationBedMapper.selectPage(pageReqVO);
    }

    @Override
    public List<AccommodationBedDO> getAccommodationBedList(AccommodationBedExportReqVO exportReqVO) {
        return accommodationBedMapper.selectList(exportReqVO);
    }

    @Override
    public List<AccommodationBedDO> getAccommodationBedListByRoomId(AccommodationBedListReqVO reqVO) {
        LambdaQueryWrapper<AccommodationBedDO> wrapper = new LambdaQueryWrapper<AccommodationBedDO>().eq(AccommodationBedDO::getRoomId, reqVO.getRoomId());
        if (ObjectUtil.isNotNull(reqVO.getOccupy())) {
            wrapper.eq(AccommodationBedDO::getOccupy, reqVO.getOccupy());
        }
        wrapper.orderByDesc(BaseDO::getCreateTime);
        return accommodationBedMapper.selectList(wrapper);
    }

    @Override
    public List<AccommodationBedDO> getAccommodationBedListByRoomId(String roomId) {
        return accommodationBedMapper.selectList(new LambdaQueryWrapperX<AccommodationBedDO>().eq(AccommodationBedDO::getRoomId, roomId));
    }

}
